angular.module('order-detail', ['row-show'])

.controller 'OrderDetailCtrl', ($localStorage,$scope,SendModal,$state, $http, $ionicHistory,$stateParams,showToast,orderInfo,$ionicPopup,userData,$cordovaPrinter) ->
    $scope.$storage = $localStorage
    console.log $localStorage['oDOrderInfo']
    getType = ->
        console.log $localStorage['oDOrderInfo']
        if $localStorage['oDOrderInfo'].paytype == '支付宝' or $localStorage['oDOrderInfo'].paytype == '微信支付'
            if $localStorage['oDOrderInfo'].orderStatus == '待发货'
                $scope.onlinePay = true
                $scope.bankPay = false
                $scope.offlinePay = false
        else if $localStorage['oDOrderInfo'].paytype == '银行转账'
            if $localStorage['oDOrderInfo'].orderStatus == '待发货' or $localStorage['oDOrderInfo'].orderStatus == '待付款'
                $scope.bankPay = true
                $scope.onlinePay = false
                $scope.offlinePay = false
        else if $localStorage['oDOrderInfo'].paytype == '货到付款'
            if $localStorage['oDOrderInfo'].orderStatus == '待发货' or $localStorage['oDOrderInfo'].orderStatus == '待付款'
                $scope.offlinePay = true
                $scope.onlinePay = false
                $scope.bankPay = false
    $scope.orderItems = []

    _.extend $scope,
        goPrinter: ->
            $http.get("http://www.upd365.com/apiforapp/printtrade/sendemail.asp?userid=#{userData.getToken()}&tradeid=#{$localStorage['oDOrderInfo'].id}")
            .success (resp) ->
                if resp?.status == '0'
                    showToast resp.msg
                else
                    showToast resp.msg
            .error ->
                showToast '您的网络又调皮了'
        goBack: ->
#            $ionicHistory.goBack()
            $state.go 'tab.orders'
        goIntelligent:(orderId) ->
            $state.go 'tab.order-status-orders',orderid:orderId
        send:() ->
            SendModal.showModal()
        remove: ->
            $http.post('/orders/',{action:'delorder',userid:userData.getToken(),id:$localStorage['oDOrderInfo'].id})
            .success (resp) ->
                if resp?.status == '0'
                    showToast resp.msg
                    $localStorage['orders'] = _.without $localStorage['orders'],$localStorage['oDOrderInfo']
                    $ionicHistory.goBack()
                else
                    showToast resp.msg
            .error ->
                showToast '您的网络又调皮了'


    $scope.$on '$ionicView.beforeEnter', ->
        $scope.onlinePay = false
        $scope.bankPay = false
        $scope.offlinePay = false
        getType()
    $scope.$on '$stateChangeSuccess', (event,toState)->
        if toState?.name=='tab.orders' #返回订单列表时,清空立即发货的展示判断
            $scope.onlinePay = false
            $scope.bankPay = false
            $scope.offlinePay = false







